(function (window, undefined) {
    var string = {
        /**
         * 判断字符串是否为空
         * @param {string} text 
         */
        isNullOrEmpty: function (text) {
            return text === "" || text === null || text === undefined;
        },
        /**
         * 判断字符串是否为空或者空白字符串
         * @param {string} text 
         */
        isNullOrWhiteSpace: function (text) {
            return text === "" || text.trim().length === 0 || text === null || text === undefined;
        }
    }

    /**
     * 获取过期时间的毫秒数
     * @param {string} timeStr 时间字符串,单位是小写字母，格式：1s(second),1m(minute),1h(hour),1d(day)
     */
    var getExpiresMilliseconds = function (timeStr) {
        var endIndex = timeStr.length - 1;
        var numStr = timeStr.substr(0, endIndex);
        var num = parseInt(numStr);//时间数值

        var unit = timeStr.substr(endIndex, 1);//时间单位
        var result = 0;
        switch (unit) {
            case "s":
                result = num * 1000;
                break;
            case "m":
                result = num * 1000 * 60;
                break;
            case "h":
                result = num * 1000 * 60 * 60;
                break;
            case "d":
                result = num * 1000 * 60 * 60 * 24;
                break;

        }
        return result;
    }

    /**
     * 
     * @param {string} name 名称
     * @param {string} value 值
     * @param {Date} expires 过期日期
     */
    var updateCookie = function (name, value, expires) {
        document.cookie = name.trim() + "=" + value.trim() + ";expires=" + expires.toGMTString();
    }

    /**
     * 添加cookie
     * @param {string} name 名称
     * @param {string} value 值
     * @param {string} expireTime 过期时间,单位是小写字母，格式：1s(second),1m(minute),1h(hour),1d(day)
     */
    var add = function (name, value, expireTime) {
        var millisecond = getExpiresMilliseconds(expireTime);
        var expTime = new Date();
        expTime.setTime(expTime.getTime() + millisecond);
        //设置cookie的名称、值、失效时间
        updateCookie(name, value, expTime);
    }

    /**
     * 获取cookie
     * @param {string} name 名称
     */
    var get = function (name) {
        //获取当前所有cookie
        var cookieStr = document.cookie;
        if (string.isNullOrWhiteSpace(cookieStr)) {
            return null;
        }
        //截取变成cookie数组
        var cookieArray = cookieStr.split(';');
        //循环每个cookie
        for (var i = 0; i < cookieArray.length; i++) {
            //将cookie截取成两部分
            var cookieItem = cookieArray[i];
            var keyValuePair = cookieItem.split("=");
            var key = keyValuePair[0].trim();
            var val = keyValuePair[1].trim();
            //判断cookie的name 是否相等
            if (key == name) {
                return val;
            }
        }
        return null;
    }
    var getCookieObj = function () {
        var obj = {};
        //获取当前所有cookie
        var cookieStr = document.cookie;
        if (string.isNullOrWhiteSpace(cookieStr)) {
            return obj;
        }
        //截取变成cookie数组
        var cookieArray = cookieStr.split(';');
        //循环每个cookie
        for (var i = 0; i < cookieArray.length; i++) {
            //将cookie截取成两部分
            var cookieItem = cookieArray[i];
            var keyValuePair = cookieItem.split("=");
            var key = keyValuePair[0].trim();
            var val = keyValuePair[1].trim();
            obj[key] = val;
        }
        return obj;
    }
    /**
     * 判断cookie是否存在
     * @param {string} name 名称 
     */
    var exist = function (name) {
        return get(name) !== null;
    }

    /**
     * 删除cookie
     * @param {string} name 名称
     */
    var remove = function (name) {
        //获取cookie是否存在
        if (!exist(name)) {
            return;
        }
        //把时间设为当前时间之前cookie就过期了
        var expTime = new Date(1970, 1, 1, 0, 0, 0, 0);
        updateCookie(name, "", expTime);
    }

    /**
     * 移除所有cookie
     */
    var removeAll = function () {
        var obj = getCookieObj();
        for (var key in obj) {
            if (obj.hasOwnProperty(key)) {
                remove(key);
            }
        }
    }
    var cookieHelper = {
        add: add,
        get: get,
        getCookieObj: getCookieObj,
        remove: remove,
        removeAll: removeAll,
        exist: exist
    }
    window.cookieHelper = cookieHelper;
})(window, undefined)

var addCookie = function () {
    var cookie_key = document.getElementById("cookie_key").value;
    var cookie_val = document.getElementById("cookie_val").value;
    var cookie_time = document.getElementById("cookie_time").value;
    cookieHelper.add(cookie_key, cookie_val, cookie_time);
    alert("添加cookie：" + cookie_key);
}

var checkCookie = function () {
    var cookie_key = document.getElementById("cookie_key").value;
    if (cookieHelper.exist(cookie_key)) {
        alert("存在cookie:" + cookie_key);
    } else {
        alert("不存在cookie:" + cookie_key);
    }
}

var deleteCookie = function () {
    var cookie_key = document.getElementById("cookie_key").value;
    cookieHelper.remove(cookie_key);
    alert("删除cookie：" + cookie_key);
}